package com.frame.pageform.job;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.*;

import com.frame.common.SystemConfig;
import com.frame.common.Xml;

public class XmlJobLoader {


	protected List load(){
		List jobs=new ArrayList();
		String jobpath=SystemConfig.getInstance().getRootPath()+"/WEB-INF/job.xml";
		Document document;
		try {
			document = Xml.createDocument(jobpath);
			Element root = document.getDocumentElement();
			NodeList nodeList = root.getChildNodes();
			int n = nodeList.getLength();
			for(int i = 0; i < n; i++)
			{
				Node node = nodeList.item(i);
				if(node instanceof Element)
				{
					Element element=(Element)node;
					NodeList nodeList2 = element.getChildNodes();
					for(int i2 = 0; i2 < nodeList2.getLength(); i2++)
					{
						Node node2 = nodeList2.item(i2);
						if(node2 instanceof Element)
						{

							Element element2=(Element)node2;
							try{
								String jobclass=element2.getAttribute("type");
								Class c=Class.forName(jobclass);
								Object obj=c.newInstance();
								String intervalstr=element2.getAttribute("interval");
								int interval=Integer.parseInt(intervalstr);
								if(interval<0)
									throw new Exception(jobclass+"的间隔时间设置错误，必须大于0(以秒为单位)");
								Job job=new Job((Runnable)obj,interval);
								jobs.add(job);
							}catch(Exception e){
								e.printStackTrace();
							}
						}
					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jobs;
	}
}
